/*
 * @(#)LogonAction.java 1.0 2012/03/06
 *
 * Copyright (c) 
 *
 * 
 */

package mock.ab_sd.web.action;

import java.util.Map;
import mock.ab_sd.business.LogonBOImpl;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * LogonAction class 
 * @author QuyGP
 * @created 15/8/2012
 */
public class LogonAction extends BaseAction{

	/** user name */
	private String userName;
	
	/** password */
	private String password;
	
	/** store error message */
	private String errorMessage;	
	
	/** constant */
	private static final long serialVersionUID = 1L;

	/**
	 * override execute method of ActionSupport
	 * This method invokes authenticate method of LogonBOImpl class
	 *    to authenticate user name and password
	 * @author QuyGP
	 * @created 15/8/2012
	 */
	@SuppressWarnings("unchecked")
	@Override
	public String execute() throws Exception {
		
		// declare variable to store error message string
		errorMessage = "";
		
		// create a LogonBOImpl object
		LogonBOImpl logonBO = new LogonBOImpl();
		
		/* if authentication of user name and password returns true,
		 * then add user name of the user to Map names session  
		 */
		if (logonBO.authenticate(userName, password)) {
			
			/* create a Map names session
			 * get session of the user and assign it to session 
			 */
			Map<String, String> session = ActionContext.getContext().getSession();
			session.put("logon", userName);
			session.put("logonid",String.valueOf(logonBO.getUser().getUserID()));
			return SUCCESS; // return the result of execution is successful
		} else {
			
			// error message
			errorMessage = "User name or password does not match database" ;					
		}

		return ERROR; // return the result of excution is unsuccessful
	}
 
	/**
	 * override validate method of ActionSupport
	 * This method validates user name and password
	 * @author QuyGP
	 * @created 15/8/2012
	 */
	@Override
	public void validate() {
		
		// if user name is empty, show error message
		if (userName.trim().length() == 0){
			addFieldError("userName", "Please enter user name");
		}
		
		// if password is empty, show error message
		if (password.trim().length() == 0) {
			addFieldError("password", "Please enter password");
		}
	}

	/**
	 * get user name
	 * @return user name
	 * @author QuyGP
	 * @created 15/8/2012
	 */
	public String getUserName() {
		return userName;
	}

	/**
	 * set user name
	 * @param userName
	 * @author QuyGP
	 * @created 15/8/2012
	 */
	public void setUserName(String userName) {
		this.userName = userName;
	}

	/**
	 * get password
	 * @return password
	 * @author QuyGP
	 * @created 15/8/2012
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * set password
	 * @param password
	 * @author QuyGP
	 * @created 15/8/2012
	 */
	public void setPassword(String password) {
		this.password = password;
	}
	
	/**
	 * get error message
	 * @return error message
	 * @author QuyGP
	 * @created 15/8/2012
	 */
	public String getErrorMessage() {
		return errorMessage;
	}

	/**
	 * set error message
	 * @param errorMessage
	 * @author QuyGP
	 * @created 15/8/2012
	 */
	public void setErrorMessage(String errorMessage) {
		this.errorMessage = errorMessage;
	}
}
